package com.antler.smt.portal.service.impl;

import com.antler.smt.entity.WishPeopleEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.portal.mapper.WishPeopleEntityMapper;
import com.antler.smt.portal.mapper.WishPeopleInfoMapper;
import com.antler.smt.request.portal.BindEmployedReq;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.portal.WishPeopleService;
import com.antler.smt.utils.DateUtil;
import com.antler.smt.utils.ReturnResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 钟意的工程师接口实现
 *
 * @author zhangyi
 * @date 2018/9/20 18:12
 */
@Service
public class WishPeopleServiceImpl implements WishPeopleService {


    @Autowired
    private WishPeopleInfoMapper wishPeopleInfoMapper;

    @Autowired
    private WishPeopleEntityMapper wishPeopleEntityMapper;

    /**
     * 获取工程师列表
     *
     * @return
     */
    @Override
    public Result getPeopleList(Long memberId) {
        BindEmployedReq bindEmployedReq = new BindEmployedReq();
        bindEmployedReq.setMemberId(memberId);
        List<List<WishPeopleEntity>> peopleList = new ArrayList<>();
        //钟意
        bindEmployedReq.setCollections(1);
        List<WishPeopleEntity> peopleGoods = wishPeopleInfoMapper.selectWishPeopleList(bindEmployedReq);
        //非钟意
        bindEmployedReq.setCollections(2);
        List<WishPeopleEntity> peopleBads = wishPeopleInfoMapper.selectWishPeopleList(bindEmployedReq);

        peopleList.add(peopleGoods);
        peopleList.add(peopleBads);
        return new Result(peopleList);
    }

    /**
     * 取消中意工程师
     *
     * @param peopleId
     * @return
     */
    @Override
    public Result cancelWishPeople(Long peopleId) {
        WishPeopleEntity wishPeopleEntity = new WishPeopleEntity();
        wishPeopleEntity.setId(peopleId);
        //取消标志位
        wishPeopleEntity.setCollections(2);
        int i = wishPeopleEntityMapper.updateByPrimaryKeySelective(wishPeopleEntity);
        return ReturnResultUtil.getResult("操作成功","操作失败",i);
    }

    /**
     * 添加钟意的工程师
     *
     * @param memberId
     * @param employed
     * @return
     */
    @Override
    public Result addPeopleList(Long memberId, Long employed) {
        if(memberId == null || employed == null){
            return new Result(ResultCodeEnum.REQUEST_PARAM_ERROR,"请求参数不正确！");
        }
        BindEmployedReq bindEmployedReq = new BindEmployedReq();
        bindEmployedReq.setEmployed(employed);
        bindEmployedReq.setMemberId(memberId);
        int i = wishPeopleInfoMapper.selectWistPeopleExist(bindEmployedReq);
        if(i == 0){
            WishPeopleEntity wishPeopleEntity = new WishPeopleEntity();
            wishPeopleEntity.setEmployed(memberId);
            wishPeopleEntity.setEmployer(employed);
            //收藏标志位1 取消2
            wishPeopleEntity.setCollections(1);
            wishPeopleEntity.setCreateTime(DateUtil.stampToDate(System.currentTimeMillis()));
            int i1 = wishPeopleEntityMapper.insertSelective(wishPeopleEntity);
            return ReturnResultUtil.getResult("钟意成功！","选择失败!",i1);
        }else{
            return new Result(ResultCodeEnum.QUERY_DATA_ERROR,"该工程师已经钟意过了！");
        }
    }
}
